/**
 * 0. Project	: com.dnbns.common 프로젝트
 *
 * 1. FileName	: AlarmSchedule.java
 * 2. Package	: com.dnbns.common
 * 3. 작성자		: 윤화현
 * 4. 작성일		: 2013. 12. 30. 오후 5:30:36
 * 5. Comment	: 
 */

package com.dnbns.common;

import java.util.Calendar;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;

import com.dnbns.bodywater.AlarmReceiver;

public class AlarmSchedule {

	private static AlarmSchedule mInstance;
	private Context mContext;
	
	public AlarmSchedule(Context mContext){
		this.mContext = mContext;
	}

	public static AlarmSchedule getInstance(Context activity){
		mInstance = new AlarmSchedule(activity);
		return mInstance;
	}
	
	public void setAlarmSchedule(int idx, int hour, int minute){
    	AlarmManager manager = (AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE);

		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.HOUR_OF_DAY, hour);
		cal.set(Calendar.MINUTE, minute);
		cal.set(Calendar.SECOND, 0);
		
    	Intent intent = new Intent(mContext, AlarmReceiver.class);
    	intent.putExtra("hour", hour);
    	intent.putExtra("minute", minute);
		PendingIntent pender = PendingIntent.getBroadcast(mContext, idx, intent, PendingIntent.FLAG_UPDATE_CURRENT);
		
		manager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 0, pender);
	}
	
	public void deleteAlarmSchedule(int idx){
		//해제
	    AlarmManager manager = (AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE);
    	Intent intents = new Intent(mContext, AlarmReceiver.class);
	    PendingIntent sender = PendingIntent.getBroadcast(mContext, idx, intents, 0);
	    manager.cancel(sender);
	}
}
